<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 支付接口
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisInfo;
use app\ais\model\AisInfoOrder;
use app\ais\model\MchId;
use app\ais\model\AisConfig;
use app\common\facade\WechatPay;
use filter\Filter;

class Infopay extends Base{

    /**
     * 初始化当前应用是否登录
     * @return void
     */
    public function initialize() {
        parent::initialize();
        $this->isUserAuth();
    }

    /**
     * 购买商品
     */
    public function index(){
        if (request()->isPost()) {
            $param['id']       = $this->request->param('id/d');
            $param['telphone'] = $this->request->param('telphone/s','');
            $param['message']  = $this->request->param('message','');
            $param['ucode']    = $this->request->param('ucode','');
            $param['fields']   = $this->request->param('fields/s','[]','htmlspecialchars_decode');
            $this->apiSign($param);
            $param['fields'] = json_encode(Filter::filter_escape(json_decode($param['fields'],true)));
            //判断是否已下架
            $item = AisInfo::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'is_lock' => 0])->find();
            if(empty($item)){
                return enjson(403,'信息不存在');
            }
            //读取订单
            $order_no = order_no($this->user->invite_code);
            $amount   = $item->price;
            //唤醒微信支付参数
            $payparm = [
                'openid'     => $this->user->miniapp_uid,
                'miniapp_id' => $this->miniapp_id,
                'name'       => $item->mp->title,
                'order_no'   => $order_no,
                'total_fee'  => $amount <= 0 ? 1 : $amount * 100,
                'notify_url' => api(1,'ais/notify/infoOrder',$this->miniapp_id),
            ];
            //读取配置
            if($this->member_miniapp->is_psp){
                $setting = AisConfig::getConfig($this->miniapp_id);
                if (empty($setting->mchid)) {
                    return enjson(403,'服务商模式下,必须配置默认商户号');
                }
                $payparm['mchid'] = $setting->mchid;  
            }
            $ispay = WechatPay::orderPay($payparm);
            if ($ispay['code'] == 0) {
                return enjson(403,$ispay['msg']);
            }
            //判断是否新订单
            $param['member_miniapp_id'] = $this->miniapp_id;
            $param['uid']               = $this->user->id;
            $param['info_uid']          = $item->uid;
            $param['info_id']           = $item->id;
            $param['phone']             = $param['telphone'];
            $param['amount']            = $amount;
            $param['order_no']          = $order_no;
            $param['message']           = $param['message'];
            $param['fields']            = $param['fields'];
            $param['cache']             = $item->toJson();
            $rel = AisInfoOrder::insertOrder($param,$order_no);
            if(empty($rel)){
                return enjson(204,'购买商品失败');
            }
            return enjson(200,'成功',$ispay['data']);
        }
    }

    /**
     * 我的订单
     * @return void
     */
    public function order(){
        $param['active']  = $this->request->param('active/d',0);
        $param['page']    = $this->request->param('page/d',1);
        $this->apiSign($param);  
        $condition['uid']     = $this->user->id;
        $condition['is_del']  = 0;
        switch ($param['active']) {
            case 1:
                $condition['paid_at'] = 0;
                break;
            case 2:
                $condition['paid_at'] = 1;
                $condition['status']  = 0;
                break;
            case 3:
                $condition['paid_at'] = 1;
                $condition['status']  = 1;
                break;
        }
        $lists = AisInfoOrder::where($condition)->field('cache',true)->order('id desc')->paginate(10)->toArray();
        $data = [];
        foreach ($lists['data'] as $key => $value) {
            $data[$key] = $value;
            $data[$key]['fields'] = json_decode($value['fields'],true);
        }
        return enjson(200,'成功',$data);
    }
}